package zhongbo.java.air.AirIngestion.listener;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import zhongbo.java.air.AirIngestion.dao.FlightPathDto;

import java.util.Date;


@Slf4j
@Component
public class RedisSubscribeLogChaneIListener implements MessageListener {

    @Autowired
    private RedisLogChaneIPublisher publisher;



    @Override
    public void onMessage(Message message, byte[] bytes) {
        byte[] messageBody = message.getBody();
        String msgString = new String(messageBody);
        log.info("接收到订阅日志的消息{}", msgString);

//        String msgString = "{\"timestamp\":1743747387840,\"icao\":\"acb84d\"}";
        doConvert(msgString);
    }

    //字段映射
    private void doConvert(String msgString) {
        //解析json字符串格式
        JSONObject jsonObject = JSONUtil.parseObj(msgString);
        FlightPathDto flightPath = new FlightPathDto();

        JSONArray traceArray = jsonObject.getJSONArray("trace");
        flightPath.setIcaoCode(jsonObject.getStr("icao"));

        // 获取timestamp
        Long timestamp = jsonObject.getLong("timestamp");
        Object o = traceArray.get(0);
        if (o != null) {
            double offset = Double.parseDouble(String.valueOf(o));
            double finalTempStamp = timestamp + offset;
            timestamp = (long) finalTempStamp;
            // 转换为字符串，判断长度
            String timestampStr = String.valueOf(timestamp);
            double time = Double.parseDouble(timestampStr);
            if (timestampStr.length() == 13) {
                // 如果长度为13，说明已经是毫秒单位，直接使用
                timestamp = (long) time;
                Date flightPathTimeStamp1 = new Date(timestamp);
                flightPath.setTimestamp(flightPathTimeStamp1);
            } else {
                // 否则，假设是秒单位，需要乘以1000转换为毫秒单位
                timestamp = (long) time;
                Date flightPathTimeStamp1 = new Date(timestamp * 1000);
                flightPath.setTimestamp(flightPathTimeStamp1);
            }
        }

            // 获取sourceType
            Object o1 = traceArray.get(8);
            if (o1 != null) {
                if (o1 instanceof JSONObject) {
                    JSONObject jo = (JSONObject) o1;
                    String type = jo.getStr("type");
                    flightPath.setSourceType(type);
                }
            }


            Object latitude = traceArray.get(1);
            Object longitude = traceArray.get(2);
            Object altitude = traceArray.get(3);
            Object speed = traceArray.get(4);
            Object heading = traceArray.get(5);


            if (latitude != null) {
                double latitude1 = Double.parseDouble(String.valueOf(latitude));
                flightPath.setLatitude(latitude1);
            }
            if (longitude != null) {
                double longitude1 = Double.parseDouble(String.valueOf(longitude));
                flightPath.setLongitude(longitude1);
            }
            if (altitude != null) {
                String str = String.valueOf(altitude);
                double altitude1 = 0;
                if ("ground".equals(str)) {
                    //
                    altitude1 = 0;
                } else {
                    altitude1 = Double.parseDouble(str);
                }
                flightPath.setAltitude(altitude1);
            }
            if (speed != null) {
                double speed1 = Double.parseDouble(String.valueOf(speed));
                flightPath.setSpeed(speed1);
            } else {
                return;
            }
            if (heading != null) {
                double heading1 = Double.parseDouble(String.valueOf(heading));
                flightPath.setHeading(heading1);
            }

            String convertedJsonStr = JSONUtil.toJsonStr(flightPath);
            log.info("转换后的数据: " + convertedJsonStr);
//        String flightPathJson = JSONUtil.toJsonStr(flightPath);
            publisher.sendMessage(convertedJsonStr);

        }

    }

//    private Double convertToDouble(Object obj) {
//        if (obj == null) {
//            return null;
//        }
//        if (obj instanceof Double) {
//            return (Double) obj;
//        } else if (obj instanceof Number) {
//            return ((Number) obj).doubleValue();
//        } else {
//            try {
//                return Double.parseDouble(String.valueOf(obj));
//            } catch (NumberFormatException e) {
//                log.error("无法将 {} 转换为 Double", obj, e);
//                return null;
//            }
//        }
//    }

